import { apiUserNameAuthentication } from '../../api/index'
let page = {
  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    idCard: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.setData({
      isIphoneX: getApp().globalData.isIphoneX
    })
  },
  getPhoneNumber (e) {
    if (e.detail.errMsg.search('ok') !== -1) {
      this.submit()
    } else {
      console.log('请授权手机号')
    }
  },
  bindName (e) {
    let ipt = this.data.userName
    const reg = /[u4E00-u9FA5]/g
    if (!reg.test(e.detail.value)) {
      ipt = e.detail.value
    } else if (!e.detail.value) {
      ipt = ''
    }
    this.setData({
      userName: ipt
    })
  },
  bindID (e) {
    this.setData({
      idCard: e.detail.value
    })
  },
  getUserReal () {
    apiUserNameAuthentication({ user_name: this.data.userName, id_card: this.data.idCard }).then(
      res => {
        console.log(res)
        wx.navigateBack({
          delta: 1
        })
      }
    )
  },
  submit () {
    if (this.data.userName) {
      if (this.data.idCard && this.data.idCard.length === 18) {
        this.getUserReal()
      } else {
        wx.showToast({
          title: '请填写正确的身份证号',
          icon: 'none'
        })
      }
    } else {
      wx.showToast({
        title: '请填写真实姓名',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  }
}
Page(page)
